<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class proajaxController extends Controller
{
    public function index(Request $request)
    {
	//判断是否登录
    //yes代表登录，no代表没登录
		if (session()->has('homeuser')){
			// echo json_encode($request->input('num'));
			$pro = DB::table('product')->where('id',$request->input('id'))->first();
	    	$user = DB::table('user')->where('id',session('homeuser')->id)->first();
	    	$id = DB::table('car')->insertGetId(
				['product_id' => "$pro->id", 'user_id' => "$user->id", 'num' =>$request->input('num'), 'pic' => "$pro->picture", 'price' => "$pro->lower_price", 'name' => "$pro->name" ]
			);
			echo json_encode('yes');
		}else{
			echo json_encode('no');
		}
		// echo json_encode(num);

    }
    //全部评价
    public function qp(Request $request)
    {	
    	$id = $request->input('id');
    	$user = DB::table('product_evaluation')
          ->join('user', 'product_evaluation.user_id', '=', 'user.id')
          ->join('evaluation_reply', 'product_evaluation.order_id', '=', 'evaluation_reply.evaluation_id')
          ->select('product_evaluation.*', 'user.nick_name', 'evaluation_reply.reply_content', 'evaluation_reply.reply_time')
          ->where('product_evaluation.product_id', $id)
          ->orderBy('evaluation_time', 'desc')
          ->paginate(4);
        echo json_encode($user);
    }
    //好评
    public function hp(Request $request)
    {	
    	$id = $request->input('id');
    	$user = DB::table('product_evaluation')
          ->join('user', 'product_evaluation.user_id', '=', 'user.id')
          ->join('evaluation_reply', 'product_evaluation.order_id', '=', 'evaluation_reply.evaluation_id')
          ->select('product_evaluation.*', 'user.nick_name', 'evaluation_reply.reply_content', 'evaluation_reply.reply_time')
          ->where('product_evaluation.product_id', $id)
          ->where('product_evaluation.title', '好评')
          ->orderBy('evaluation_time', 'desc')
          ->paginate(4);
        echo json_encode($user);
    }
    //中评
    public function zp(Request $request)
    {	
    	$id = $request->input('id');
    	$user = DB::table('product_evaluation')
          ->join('user', 'product_evaluation.user_id', '=', 'user.id')
          ->join('evaluation_reply', 'product_evaluation.order_id', '=', 'evaluation_reply.evaluation_id')
          ->select('product_evaluation.*', 'user.nick_name', 'evaluation_reply.reply_content', 'evaluation_reply.reply_time')
          ->where('product_evaluation.product_id', $id)
          ->where('product_evaluation.title', '中评')
          ->orderBy('evaluation_time', 'desc')
          ->paginate(4);
        echo json_encode($user);
    }
    //差评
    public function cp(Request $request)
    {	
    	$id = $request->input('id');
    	$user = DB::table('product_evaluation')
          ->join('user', 'product_evaluation.user_id', '=', 'user.id')
          ->join('evaluation_reply', 'product_evaluation.order_id', '=', 'evaluation_reply.evaluation_id')
          ->select('product_evaluation.*', 'user.nick_name', 'evaluation_reply.reply_content', 'evaluation_reply.reply_time')
          ->where('product_evaluation.product_id', $id)
          ->where('product_evaluation.title', '差评')
          ->orderBy('evaluation_time', 'desc')
          ->paginate(4);
        echo json_encode($user);
    }

    public function shopcar(Request $request)
    {	
    	// dd($id);
    	// dd(session('homeuser')->id);
  //   	if(session()->has('homeuser')){
  //   		$pro = DB::table('product')->where('id',$id)->first();
	 //    	$user = DB::table('user')->where('id',session('homeuser')->id)->first();
	 //    	$id = DB::table('car')->insertGetId(
		// 		['product_id' => '$pro->id', 'user_id' => '$user->id', 'num' => ]
		// 	);

		// 	return view('car.successtocar');
		// }else{
		// 	return redirect('/home/gologin');
		// }
		// echo json_encode($request->input('num'));
		return view('car.successtocar');

    }
    //联系客服的ajax
    public function chat(Request $request){
    	$content = $request->input('content');
    	if(session()->has('homeuser')){
    		$user_id = session('homeuser')->id;
    		DB::table('chat')->insertGetId(
				['user_id' => $user_id, 'content' => $content]
			);
    	}else{
    		DB::table('chat')->insertGetId(['content' => $content]);
    	}
    	echo json_encode($content);
    }
    public function get_chat(Request $request){
    	$data = DB::table('chat')->get();
    	echo json_encode($data);
    }
    
}
